package com.backend.mzyf.filter;

import com.backend.mzyf.Utils.Security;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class LoadCsrfFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String token = null;

        token = (String) request.getSession().getAttribute("csrf");

        if (null == token){
            token = new Security().generaCsrfToken();
            request.getSession().setAttribute("csrf",token);
        }
        System.out.println(token);
        request.setAttribute("csrf",token);

        filterChain.doFilter(servletRequest,servletResponse);

    }

    public void destroy() {

    }
}
